/**
 * 
 */
package cw.serviceImp;

import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cw.entity.ArrangeGroup;
import cw.entity.Group;
import cw.entity.Team;
import cw.mapper.ArrangeGroupMapper;
import cw.mapper.GroupMapper;
import cw.mapper.TeamMapper;
import cw.service.ArrangeGroupService;

/**
 * @author Administrator
 *
 */
@Service("arrangeGroupService")
public class ArrangeGroupServiceImpl implements ArrangeGroupService {

	@Resource
	private ArrangeGroupMapper arrangeGroupMapper;
	
	@Resource
	private GroupMapper groupMapper;
	
	@Resource
	private TeamMapper teamMapper;
	
	/* (non-Javadoc)
	 * @see cw.service.ArrangeGroupService#insert(cw.entity.ArrangeGroup)
	 */
	@Override
	public int insert(ArrangeGroup record) {
		// TODO Auto-generated method stub
		return arrangeGroupMapper.insert(record);
	}

	/* (non-Javadoc)
	 * @see cw.service.ArrangeGroupService#insertSelective(cw.entity.ArrangeGroup)
	 */
	@Override
	public int insertSelective(ArrangeGroup record) {
		// TODO Auto-generated method stub
		return arrangeGroupMapper.insertSelective(record);
	}

	/* (non-Javadoc)
	 * @see cw.service.ArrangeGroupService#deleteByPrimaryKey()
	 */
	@Override
	public int deleteByPrimaryKey() {
		// TODO Auto-generated method stub
		return arrangeGroupMapper.deleteByPrimaryKey();
	}

	/* (non-Javadoc)
	 * @see cw.service.ArrangeGroupService#queryGroup(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<ArrangeGroup> queryArrangeGroup(String groupId, String description,
			String id, Integer offset, Integer pageSize) {
		// TODO Auto-generated method stub
		return arrangeGroupMapper.queryArrangeGroup(groupId, description, id, offset, pageSize);
	}

	/* (non-Javadoc)
	 * @see cw.service.ArrangeGroupService#getTotal(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public Integer getTotal(String groupId, String description, String id) {
		// TODO Auto-generated method stub
		return arrangeGroupMapper.getTotal(groupId, description, id);
	}

	@Override
	public String arrangeGroup(int num) {
		// TODO Auto-generated method stub
		
		//得到所有的球队
    	List<Team> listTeam = teamMapper.query();   	 	
    	//球队总数
    	int totalTeam = listTeam.size();
    	
    	//每组有几队
    	int groupNum = totalTeam / num;
    	
    	//得到所有的分组安排
    	List<ArrangeGroup> listArrangeGroup =  arrangeGroupMapper.query();
    	
    	if(listArrangeGroup.size()!=0 &&listArrangeGroup!=null){
    		arrangeGroupMapper.deleteByPrimaryKey();
    	}
    	//得到所有的组
    	List<Group> listGroup1 = groupMapper.query();
    	if(listGroup1.size()!=0 &&listGroup1!=null){
    		groupMapper.deleteAll();
    	}
    	 
    	//创建分组，插入数据库：根据组数创建分组
    	for(int i=0;i<num;i++){
    		Group group = new Group();
    		group.setGroupName("第"+(i+1)+"组");
    		groupMapper.insertSelective(group);
    	}   
    	//得到所有的组
    	List<Group> listGroup = groupMapper.query();  	
    	
    	//得到数据库中的组数
    	int totalGroup = listGroup.size();
    	
    	//随机分组
		Random ran = new Random();
		Team team = new Team();
		Group group  = new Group();		
        for(int i = 0;i<totalGroup;i++){
        	group = listGroup.get(ran.nextInt(listGroup.size()));
    		for(int j = 0;j<groupNum;j++){
                team = listTeam.get(ran.nextInt(listTeam.size()));
                ArrangeGroup arrangeGroup = new ArrangeGroup();
                arrangeGroup.setTeamId(team.getId());
                arrangeGroup.setGroupId(group.getId());
                arrangeGroupMapper.insertSelective(arrangeGroup);
                listTeam.remove(team);
              }
    		listGroup.remove(group);
        }
        if(listTeam.size()>0){
        	listGroup = groupMapper.query(); 
        	for(int i = 0;i<num;i++){
            	group = listGroup.get(ran.nextInt(listGroup.size()));
                team = listTeam.get(ran.nextInt(listTeam.size()));
                ArrangeGroup arrangeGroup = new ArrangeGroup();
                arrangeGroup.setTeamId(team.getId());
                arrangeGroup.setGroupId(group.getId());
                arrangeGroupMapper.insertSelective(arrangeGroup);
                listTeam.remove(team);
        		listGroup.remove(group);
        		if(listTeam.size()<1){
        			break;
        		}
            }
        }
        return "success";
	}

	@Override
	public List<ArrangeGroup> query() {
		// TODO Auto-generated method stub
		return arrangeGroupMapper.query();
	}

	@Override
	public List<Integer> querGroupId() {
		// TODO Auto-generated method stub
		return arrangeGroupMapper.queryGroupId();
	}

	@Override
	public List<Integer> queryGroup(Integer groupId) {
		// TODO Auto-generated method stub
		return arrangeGroupMapper.queryGroup(groupId);
	}

}
